import logging
import time


class Config:
    arg = None


class Log:
    # 创建日志记录器
    logger = logging.getLogger("MyLogger")
    logger.setLevel(logging.INFO)


def get_time() -> str:
    return time.strftime("%Y%m%d_%H_%M_%S", time.localtime())


# 配置日志记录系统，使得日志消息可以同时输出到控制台上
def stream_handle():
    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(message)s')
    stream_handler.setFormatter(formatter)
    Log.logger.addHandler(stream_handler)


# 日志消息将被输出到指定的日志文件
def file_handle():
    file_handler = logging.FileHandler(
        filename='log/' + Config.arg['Log']['filename'] + "_{}".format(get_time()) + '.log')
    file_handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(message)s')
    file_handler.setFormatter(formatter)
    Log.logger.addHandler(file_handler)


# def add_handle():
#     stream_handle()
#     file_handle()
def add_handle():
    # 先移除已经添加的处理器
    for handler in Log.logger.handlers[:]:
        Log.logger.removeHandler(handler)

    # 添加流处理器和文件处理器
    stream_handle()
    file_handle()
